var http = require('http');
var fs = require('fs');
var url = require('url');

function read(cb) {
    fs.readFile('./getlist.json', 'utf8', (err, data) => {
        if (err || data.length == 0) {
            cb([]);
        } else {
            cb(JSON.parse(data));
        }
    });
};
function readTab(cb) {
    fs.readFile('./tab.json', 'utf8', (err, data) => {
        if (err || data.length == 0) {
            cb([]);
        } else {
            cb(JSON.parse(data));
        }
    });
};

var server = http.createServer((req, res) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.setHeader("X-Powered-By", ' 3.2.1');
    if (req.method === 'OPTIONS') return res.end();
    var { pathname, query } = url.parse(req.url, true);
    if (pathname === '/getlist') {
        res.setHeader('Content-type', 'application/json;charset=utf8');
        var { pageSize, page } = query;
        var arr = [];
        read(function (list) {
            var pageMax = Math.round(list.length / pageSize);
            if (page <= pageMax) {
                for (var i = (page - 1) * pageSize; i <= page * pageSize - 1; i++) {
                    if (i < list.length) {
                        arr.push(list[i]);
                    }
                }
            }
            res.end(JSON.stringify({
                "status": "success",
                "result": {
                    "code": 10002,
                    "msg": '返回成功',
                    "data": arr,
                },
            }))
        })
    };
    if (pathname === '/tab') {
        res.setHeader('Content-type', 'application/json;charset=utf8');
        readTab(function (list) {
            res.end(JSON.stringify(list));
        })
    };
    if (pathname === '/tablist') {
        res.setHeader('Content-type', 'application/json;charset=utf8');
        var { pageSize, page, key } = query;
        var arr = [];
        read(function (list) {
            if (key == 1) {
                list = list.filter(item => {
                    return item.id % 3 == 0
                })
            } else if (key == 2) {
                list = list.filter(item => {
                    return item.id % 2 == 0
                })
            } else if (key == 3) {
                list = list.filter(item => {
                    return item.id % 5 == 0
                })
            }
            var pageMax = Math.round(list.length / pageSize);
            if (page <= pageMax) {
                for (var i = (page - 1) * pageSize; i <= page * pageSize - 1; i++) {
                    if (i < list.length) {
                        arr.push(list[i]);
                    }
                }
            }
            res.end(JSON.stringify({
                "status": "success",
                "result": {
                    "code": 10002,
                    "msg": '返回成功',
                    "data": arr,
                },
            }))
        })
    };
    if (pathname === '/searchlist') {
        res.setHeader('Content-type', 'application/json;charset=utf8');
        var { pageSize, page, keycode } = query;
        var arr = [];
        read(function (list) {
            list = list.filter(item=>{
                return item.title.indexOf(keycode) !== -1 || item.content.indexOf(keycode) !== -1
            });
            var pageMax = Math.round(list.length / pageSize);
            if (page <= pageMax) {
                for (var i = (page - 1) * pageSize; i <= page * pageSize - 1; i++) {
                    if (i < list.length) {
                        arr.push(list[i]);
                    }
                }
            }
            res.end(JSON.stringify({
                "status": "success",
                "result": {
                    "code": 10002,
                    "msg": '返回成功',
                    "data": arr,
                },
            }))
        })
    }
});

server.listen(9090);